const md5 = require('./md5')
/**
 * 生成签名
 * @param params
 * @returns {string}
 */
function create_sign(params) {
    params = sort_ascii(params);
    return md5(params);
  }
  /**
   * 按ascii码从小到大排序
   * @param obj
   * @returns {string}
   */
  function sort_ascii(obj) {
    let arr = new Array();
    let num = 0;
    for (let i in obj) {
      arr[num] = i;
      num++;
    }
    let sortArr = arr.sort();
    //let sortObj = {};    //完成排序值
    let str = '';             //自定义排序字符串
    for (let i in sortArr) {
      str += sortArr[i] + '=' + obj[sortArr[i]] + '&';
      //sortObj[sortArr[i]] = obj[sortArr[i]];
    }
    //去除两侧字符串
    let char = '&'
    str = str.replace(new RegExp('^\\' + char + '+|\\' + char + '+$', 'g'), '');
  
    return str;
    //return sortObj;
  }
  module.exports = create_sign